package br.com.amadeus.android.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.text.format.DateFormat;
import android.util.Log;

public class Util {
	public static final String FEIRA_ID_EXTRA = "feira_id";
	public static final String FORMATO_DATA_BD = "yyyy-MM-dd HH:mm";
	public static final String FORMATO_DATA_UI = "dd/MM/yyyy HH:mm";

	public static String getString(Date date, String format) {
		return (String) DateFormat.format(format, date);
	}

	public static Date getDate(String dbDate, String format) {

		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date data = null;
		try {
			String nDbDate = dbDate.replace("T", " ");
			data = dateFormat.parse(nDbDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return data;
	}

	public static String getDouble(double d) {
		DecimalFormat decFormat = new DecimalFormat("0.00");
		return decFormat.format(d);
	}
	
	public static GregorianCalendar stringDatetoGregorianCalendar(String date){
		date = date.trim();
		String datahora1[] = date.split(" ");
		Log.i("CHART", date);
		
		int day = Integer.parseInt(datahora1[0].split("/")[0]);
		int month = Integer.parseInt(datahora1[0].split("/")[1]);
		int year = Integer.parseInt(datahora1[0].split("/")[2]);
		Log.i("11111111", datahora1[0]);
		Log.i("22222222", datahora1[1]);
		int hour = Integer.parseInt(datahora1[1].split(":")[0]);
		int minute = Integer.parseInt(datahora1[1].split(":")[1]);
		
		GregorianCalendar gregorianDate = new GregorianCalendar(year, month-1, day, hour, minute, 0);
		return gregorianDate;
		
	}
	
	public static int getIntervalHours(GregorianCalendar beginDate , GregorianCalendar endDate){
		long l1 = beginDate.getTimeInMillis();
		long l2 = endDate.getTimeInMillis();
		return (int)(l2-l1)/(1000*60*60);
	}
	
	public static String getDataHoraFormatoCompleto(Date date){
		String formatoCompleto = "";
		
		java.text.DateFormat format = java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL, new Locale("pt", "BR"));
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		StringBuilder sb = new StringBuilder();
		sb.append(c.get(Calendar.HOUR_OF_DAY));
		sb.append(":");
		int m = c.get(Calendar.MINUTE);
		String minute = Integer.toString(m);
		if (minute.length() == 1) {
			minute = "0" + minute;
		}
		sb.append(minute);
		String data = format.format(date);
		String letra = data.substring(0, 1);
		String dataPrimeiraLetraMaiscula = letra.toUpperCase() + removeCharAt(data, 0);
		
		formatoCompleto = dataPrimeiraLetraMaiscula + " às " + sb.toString();
		
		return formatoCompleto;
	}
	
	private static String removeCharAt(String s, int pos) {
	   return s.substring(0,pos)+s.substring(pos+1);
	}

}
